package com.ruoyi.manage.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUIDUtils;
import com.ruoyi.manage.domain.GoodsShelves;
import com.ruoyi.manage.domain.vo.VmVo;
import com.ruoyi.manage.mapper.GoodsShelvesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.manage.mapper.VmMapper;
import com.ruoyi.manage.domain.Vm;
import com.ruoyi.manage.service.IVmService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备Service业务层处理
 * 
 * @author zzg
 * @date 2025-03-22
 */
@Service
public class VmServiceImpl implements IVmService 
{
    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private GoodsShelvesMapper goodsShelvesMapper;

    /**
     * 查询设备
     * 
     * @param id 设备主键
     * @return 设备
     */
    @Override
    public Vm selectVmById(Long id)
    {
        return vmMapper.selectVmById(id);
    }

    /**
     * 查询设备列表
     * 
     * @param vm 设备
     * @return 设备
     */
    @Override
    public List<Vm> selectVmList(Vm vm)
    {
        return vmMapper.selectVmList(vm);
    }

    @Override
    public List<VmVo> selectVmVoList(Vm vm) {
        return vmMapper.selectVmVoList(vm);
    }

    /**
     * 新增设备
     * 
     * @param vm 设备
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertVm(Vm vm) {
        /*向设备表插入一条数据*/
        vm.setDeviceNumber(UUIDUtils.getUUID());
        vm.setCreateTime(DateUtils.getNowDate());
        int result = vmMapper.insertVm(vm);
        /*向货架表插入多条数据*/
        List<GoodsShelves> list = new ArrayList<>();
        for (int i = 1; i <= vm.getNumRows(); i++) {
            for (int j = 1; j <= vm.getNumCols(); j++) {
                GoodsShelves goodsShelves = GoodsShelves.builder()
                        .vmId(vm.getId())
                        .deviceNumber(vm.getDeviceNumber())
                        .shelfNumber(i + "-" + j)
                        .shelfStatus(2)
                        .build();
                goodsShelves.setCreateTime(DateUtils.getNowDate());
                list.add(goodsShelves);
            }
        }
        goodsShelvesMapper.insertGoodsShelvesList(list);
        return result;
    }

    /**
     * 修改设备
     * 
     * @param vm 设备
     * @return 结果
     */
    @Override
    public int updateVm(Vm vm)
    {
        vm.setUpdateTime(DateUtils.getNowDate());
        return vmMapper.updateVm(vm);
    }

    /**
     * 批量删除设备
     * 
     * @param ids 需要删除的设备主键
     * @return 结果
     */
    @Override
    public int deleteVmByIds(Long[] ids)
    {
        return vmMapper.deleteVmByIds(ids);
    }

    /**
     * 删除设备信息
     * 
     * @param id 设备主键
     * @return 结果
     */
    @Override
    public int deleteVmById(Long id)
    {
        return vmMapper.deleteVmById(id);
    }
}
